package com.hm.shopping.service;

import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.querydsl.QPageRequest;
import org.springframework.stereotype.Service;

import com.hm.common.def.DataStatusDef;
import com.hm.common.exception.ErrorCode;
import com.hm.common.exception.ServiceException;
import com.hm.common.su.bean.PageInfo;
import com.hm.common.su.bean.PageInfo.PageInfoParam;
import com.hm.common.util.CommonUtil;
import com.hm.common.util.Optional;
import com.hm.shopping.auto.su.BaseDomain;
import com.hm.shopping.def.AdvertisingLocationEnum;
import com.hm.shopping.domain.AdvertisingDomain;
import com.hm.shopping.domain.QAdvertisingDomain;
import com.hm.shopping.repository.AdvertisingRepository;
import com.hm.shopping.su.BeanUtil;
import com.hm.shopping.vo.AdvertisingVo;
import com.querydsl.core.types.dsl.BooleanExpression;

/**
 * @author shishun.wang
 * @date 2018年1月19日 下午3:43:57
 * @version 1.0
 * @describe
 */
@Service
public class AdvertisingServiceImpl implements AdvertisingService {

	@Autowired
	private AdvertisingRepository advertisingRepository;

	@Override
	public void addAdvertising(AdvertisingVo advertisingVo) {
		if (CommonUtil.isAnyEmpty(advertisingVo.getAdvertisingTitle(), advertisingVo.getBanner(), advertisingVo.getAdvertisingLocation(), advertisingVo.getLinkUri())) {
			throw ServiceException.warning(ErrorCode.REQUIRED_PARAMETERS_MISSING);
		}

		AdvertisingDomain domain = new AdvertisingDomain();
		BeanUtil.copyProperties(advertisingVo, domain);
		BaseDomain.initBaseInfo(domain);
		advertisingRepository.save(domain);
	}

	@Override
	public List<AdvertisingVo> getAdvertisings(AdvertisingLocationEnum advertisingLocation, int size) {
		QAdvertisingDomain qAdvertisingDomain = QAdvertisingDomain.advertisingDomain;
		Pageable pageable = new QPageRequest(0, size, qAdvertisingDomain.createTime.desc());
		return Optional.of(advertisingRepository.findAll(qAdvertisingDomain.advertisingLocation.eq(advertisingLocation).and(qAdvertisingDomain.status.eq(DataStatusDef.ENABLE)), pageable).getContent()).map(items -> {
			return items.parallelStream().map(item -> {
				AdvertisingVo vo = new AdvertisingVo();
				BeanUtil.copyProperties(item, vo);

				return vo;
			}).collect(Collectors.toList());
		}).get();
	}

	@Override
	public PageInfo<AdvertisingVo> queryAdvertising(PageInfoParam pageParam, AdvertisingLocationEnum advertisingLocation, String advertisingTitle) {
		QAdvertisingDomain qAdvertisingDomain = QAdvertisingDomain.advertisingDomain;
		Pageable pageable = new QPageRequest(pageParam.getPage(), pageParam.getSize(), qAdvertisingDomain.createTime.desc());

		BooleanExpression predicate = qAdvertisingDomain.status.eq(DataStatusDef.ENABLE);
		if (CommonUtil.isNotEmpty(advertisingTitle)) {
			predicate = predicate.and(qAdvertisingDomain.advertisingTitle.like(advertisingTitle));
		}

		if (CommonUtil.isNotEmpty(advertisingLocation)) {
			predicate = predicate.and(qAdvertisingDomain.advertisingLocation.eq(advertisingLocation));
		}

		Page<AdvertisingDomain> page = advertisingRepository.findAll(predicate, pageable);

		return new PageInfo<AdvertisingVo>(page.getContent().parallelStream().map(item -> {
			AdvertisingVo vo = new AdvertisingVo();
			BeanUtil.copyProperties(item, vo);
			return vo;
		}).collect(Collectors.toList()), page.getTotalPages(), page.getTotalElements());
	}

	@Override
	public void updateAdvertising(AdvertisingVo advertisingVo) {
		if (CommonUtil.isAnyEmpty(advertisingVo.getId(), advertisingVo.getAdvertisingTitle(), advertisingVo.getBanner(), advertisingVo.getAdvertisingLocation(), advertisingVo.getLinkUri())) {
			throw ServiceException.warning(ErrorCode.REQUIRED_PARAMETERS_MISSING);
		}

		AdvertisingDomain advertisingDomain = advertisingRepository.findOne(advertisingVo.getId());
		if (CommonUtil.isEmpty(advertisingDomain)) {
			throw ServiceException.warning(ErrorCode.DATA_NOT_FOUND);
		}

		advertisingDomain.setAdvertisingTitle(advertisingVo.getAdvertisingTitle());
		advertisingDomain.setBanner(advertisingVo.getBanner());
		advertisingDomain.setAdvertisingLocation(advertisingVo.getAdvertisingLocation());
		advertisingDomain.setLinkUri(advertisingVo.getLinkUri());

		advertisingRepository.save(advertisingDomain);
	}

	@Override
	public AdvertisingVo getAdvertising(String advertisingId) {
		if (CommonUtil.isEmpty(advertisingId)) {
			throw ServiceException.warning(ErrorCode.REQUIRED_PARAMETERS_MISSING);
		}

		AdvertisingDomain advertisingDomain = advertisingRepository.findOne(advertisingId);
		if (CommonUtil.isEmpty(advertisingDomain)) {
			throw ServiceException.warning(ErrorCode.DATA_NOT_FOUND);
		}

		AdvertisingVo vo = new AdvertisingVo();
		BeanUtil.copyProperties(advertisingDomain, vo);

		return vo;
	}

	@Override
	public void deleteAdvertising(String advertisingId) {
		if (CommonUtil.isEmpty(advertisingId)) {
			throw ServiceException.warning(ErrorCode.REQUIRED_PARAMETERS_MISSING);
		}

		AdvertisingDomain advertisingDomain = advertisingRepository.findOne(advertisingId);
		if (CommonUtil.isEmpty(advertisingDomain)) {
			throw ServiceException.warning(ErrorCode.DATA_NOT_FOUND);
		}
		advertisingDomain.setStatus(DataStatusDef.DISABLE);

		advertisingRepository.save(advertisingDomain);
	}

}
